/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.AA中等题.替换字符串中的括号内容;

import java.util.HashMap;
import java.util.List;

public class Main {
    HashMap<String,String> data=new HashMap<>();
    public String evaluate(String s, List<List<String>> knowledge) {


        for (List<String> strings : knowledge) {
            data.put(strings.get(0), strings.get(1));
        }

        StringBuilder stringBuilder=new StringBuilder();
        boolean flag=false;
        StringBuilder middle;
        middle = new StringBuilder();
        for (char c : s.toCharArray()) {
            switch (c){
                case '(':
                    middle = new StringBuilder();
                    flag=true;
                    break;
                case ')':
                    stringBuilder.append(plus(middle.toString()));
                    flag=false;
                    break;
                default:
                    if (!flag)
                        stringBuilder.append(c);
                    else middle.append(c);
            }
        }
        if (flag)
            stringBuilder.append(plus(middle.toString()));
        return stringBuilder.toString();
    }

    private String plus(String toString) {
        return data.getOrDefault(toString, "?");
    }

}
